<template>
  <div class="test">
    <!-- 按钮 -->
    <div class="btns">
      <el-button size="small" type="primary" @click="toAdd">添加</el-button>
      <el-button size="small" type="primary" @click="toSelect">搜索</el-button>
    </div>
    <!-- 表格 -->
    
    <el-table :data="list" style="width: 100%" size="small">
      <el-table-column type="index" label="序号"> </el-table-column>
      
      <el-table-column prop="username" label="用户名" width="180"> </el-table-column>
      <el-table-column prop="id" label="用户Uid" > </el-table-column>
      <el-table-column prop="password" label="密码"> </el-table-column>
	  <el-table-column prop="telephone" label="手机号码"> </el-table-column>
	  <el-table-column prop="gender" label="性别"> </el-table-column>
	  <el-table-column prop="realname" label="真实姓名"> </el-table-column>
	  <el-table-column prop="photo" label="照片"> </el-table-column>
	  <el-table-column prop="status" label="用户状态"> </el-table-column>
	  <el-table-column prop="role_id" label="用户角色"> </el-table-column>

      <el-table-column label="操作" width="150" align="center"> 
        <template v-slot="scope">
          <el-button size="small" type="danger" @click="del(scope.row)">删除</el-button>
          <el-button size="small" type="primary" @click="edit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 模态框 -->
    <el-dialog 
      class="customer_modal"
      title="搜索Test"
      :visible.sync="dialogVisible1"
      width="60%">
       
     <el-form :model="form1" label-width="90px">
       <el-form-item label="用户Uid" >
          <el-input v-model="form1.id"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="form2" label-width="90px">
       <el-form-item label="用户名" >
          <el-input readonly="readonly" v-model="form2.username"></el-input>
        </el-form-item>
       <el-form-item label="真实姓名" >
          <el-input readonly="readonly" v-model="form2.realname"></el-input>
        </el-form-item>
       <el-form-item label="状态" >
          <el-input readonly="readonly" v-model="form2.status"></el-input>
        </el-form-item>
       <el-form-item label="用户角色" >
          <el-input readonly="readonly" v-model="form2.roleId"></el-input>
        </el-form-item>
      </el-form>
      

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false" size="small">取 消</el-button>
        <el-button type="primary" @click="selectById" size="small">查 找</el-button>
      </span>


    </el-dialog>

    <el-dialog
      class="customer_modal"
      title="添加Test"
      :visible.sync="dialogVisible"
      width="60%">
     
      <el-form :model="form" label-width="100px">
        <el-form-item label="用户名" >
          <el-input v-model="form.username"></el-input>
        </el-form-item> 
        <el-form-item label="用户Uid" >
          <el-input v-model="form.id"></el-input>
        </el-form-item>
        <el-form-item label="密码" >
          <el-input v-model="form.password"></el-input>
        </el-form-item>
		<el-form-item label="电话号码" >
          <el-input v-model="form.telephone"></el-input>
        </el-form-item>
		<el-form-item label="性别" >
          <el-input v-model="form.gend1"></el-input>
        </el-form-item>
		<el-form-item label="真实姓名" >
          <el-input v-model="form.realname"></el-input>
        </el-form-item>
		<el-form-item label="照片" >
          <el-input v-model="form.photo"></el-input>
        </el-form-item>
		<el-form-item label="状态" >
          <el-input v-model="form.status"></el-input>
        </el-form-item>
		<el-form-item label="用户角色" >
          <el-input v-model="form.role_id"></el-input>
        </el-form-item>

      </el-form>
      
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="save" size="small">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
// 导入request , 浏览器提供给我们的用于与后台进行交互的工具 api
import {get,post} from '@/utils/request'
export default {
  data (){
    return {
      form:{},
      form1:{},
      form2:{},
      list:[],
      list1:[],
      dialogVisible:false,
      dialogVisible1:false
    }
  },
  created(){
    this.loadData();
  },
  methods:{
    // 保存
    save(){
      let url = "/user/saveOrUpdate"
      post(url,this.form).then( (response)=>{
        // 1. 模态框关闭
        this.dialogVisible = false;
        // 2. 提示
        this.$message({type:"success",message:response.message})
        // 3. 刷新
        this.loadData();
        this.form ={}
      } )
    },
    // 删除
    del(row){
		
      let url = "/user/deleteById"
      get(url,{id:row.id}).then( (response)=>{
        // 1. 提示
        this.$message({type:"success",message:response.message})
        // 2. 刷新 
        this.loadData();
      } )
    },
    // 编辑
    edit(row){
       this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
    },
    // 加载数据
    loadData(){
      let url = "/user/findAll";
      get(url).then( (response)=>{
        // response就是后端返回给我们的Message{status,message,data,timestamp}
        this.list = response.data;
      } );
    },
    selectById(){
        let url="/user/selectById";
        get(url, {id:this.form1.id}).then((response)=>{
          if (response.data[0] == null){
            this.form2 = {}
          }else{
            this.form2=response.data[0];
          }
        });
    },
    toAdd(){
      this.dialogVisible = true;
    },
    toSelect(){
      this.dialogVisible1 = true;
    }
  }
}
</script>

<style scoped>

</style>
